package com.audaque.springboot.foshanupload.mp.injector;

import com.audaque.springboot.foshanupload.mp.handler.impl.DiyTenantLineHandlerImpl;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteBatchByIds;
import com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteByIdWithFill;

import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2023-04-03 20:50:45
 */
public class DiySqlInjector extends DefaultSqlInjector {
    private DiyTenantLineHandlerImpl diyTenantLineHandlerImpl;

    public DiySqlInjector(DiyTenantLineHandlerImpl diyTenantLineHandlerImpl) {
        this.diyTenantLineHandlerImpl = diyTenantLineHandlerImpl;
    }

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        //更新时自动填充的字段，不用插入值
        methodList.add(new InsertBatchSomeColumn(i -> {
            boolean result = true;
            if (i.getFieldFill() == FieldFill.UPDATE) {
                result = false;
            } else {
                if (diyTenantLineHandlerImpl != null) {
                    String tenantIdColumn = diyTenantLineHandlerImpl.getTenantIdColumn();
                    String column = i.getColumn();
                    if (column.equalsIgnoreCase(tenantIdColumn)) {
                        result = false;
                    }
                }

            }

            return result;


        }));

        methodList.add(new AlwaysUpdateSomeColumnById());
        methodList.add(new LogicDeleteByIdWithFill());
        methodList.add(new LogicDeleteBatchByIds());

        return methodList;
    }
}
